// $Id: xml.c,v 1.1.1.1 2012/02/17 03:45:26 hzheng Exp $

#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "util.h"
#include "log.h"

#include "xml.h"

int evcpe_xml_add_indent(struct evbuffer *buffer,
		const char *indent, unsigned int count)
{
	int rc;
	unsigned int i;

	for (i = 0; i < count; i ++) {
		if ((rc = evcpe_add_buffer(buffer, "%s", indent))) {
			evcpe_error(__func__, "failed to append buffer");
			goto finally;
		}
	}
	rc = 0;

finally:
	return rc;
}

int evcpe_xml_add_string(struct evbuffer *buffer,
		const char *node, const char *string)
{
	return evcpe_add_buffer(buffer, "<%s>%s</%s>\n", node, string, node);
}

int evcpe_xml_add_int(struct evbuffer *buffer,
		const char *node, int value)
{
	return evcpe_add_buffer(buffer, "<%s>%d</%s>\n", node, value, node);
}

//int evcpe_xml_add_datetime(struct evbuffer *buffer,
//		const char *node, struct tm *datetime)
//{
//	int len;
//	char buf[32];
//
//	evcpe_debug(__func__, "adding date time node <%s>", node);
//	// TODO: timezone
//	len = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", datetime);
//	return evcpe_xml_add_string(buffer, node, buf);
//}

int evcpe_xml_add_xsd_string(struct evbuffer *buffer,
		const char *node, const char *string)
{
	return evcpe_add_buffer(buffer, "<%s xsi:type=\"xsd:string\">%s</%s>\n",
			node, string, node);
/*	return evcpe_add_buffer(buffer, "<%s>%s</%s>\n",
			node, string, node);*/
}

int evcpe_xml_add_xsd_boolean(struct evbuffer *buffer,
		const char *node, int value)
{
	return evcpe_add_buffer(buffer,
			"<%s xsi:type=\"xsd:boolean\">%s</%s>\n",
		node, value ? "1" : "0", node);	 
/*	return evcpe_add_buffer(buffer,
			"<%s>%d</%s>\n",
			node, value, node);*/
}

int evcpe_xml_add_xsd_int(struct evbuffer *buffer,
		const char *node, int value)
{
	return evcpe_add_buffer(buffer,
			"<%s xsi:type=\"xsd:int\">%d</%s>\n", node, value, node);
}

int evcpe_xml_add_xsd_unsigned_int(struct evbuffer *buffer,
		const char *node, unsigned int value)
{
	return evcpe_add_buffer(buffer,
			"<%s xsi:type=\"xsd:unsignedInt\">%d</%s>\n", node, value, node);
}
